package Main;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;

import com.mysql.jdbc.Statement;

import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import java.awt.List;

import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;

public class Frame1 extends JFrame {

	private JPanel contentPane;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Frame1 frame = new Frame1();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Frame1() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 461);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("请输入您要购买的商品id：");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		lblNewLabel.setForeground(Color.BLUE);
		lblNewLabel.setBounds(10, 280, 273, 59);
		contentPane.add(lblNewLabel);
		JTextField textField_1 = new JTextField();
		String value = textField_1.getText().trim();
		textField = new JTextField();
		Menu m = new Menu();

		textField.setBounds(10, 10, 414, 59);
		contentPane.add(textField);
		textField.setColumns(10);
		
		System.out.println(value);
		textField_1.setBounds(278, 295, 73, 38);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnNewButton = new JButton("购买");
		AddCarGoods g = new AddCarGoods();
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				g.Addcargoods(Integer.parseInt(textField_1.getText().trim()));
			}
		});
		btnNewButton.setBounds(359, 274, 65, 59);
		contentPane.add(btnNewButton);
		
		JTextArea textArea = new JTextArea();
		textArea.setBounds(10, 79, 414, 156);
		contentPane.add(textArea);
		
		JButton btnNewButton_1 = new JButton("查询可购买商品");
		btnNewButton_1.addMouseListener(new MouseAdapter() {
			String url = "jdbc:mysql://localhost:3306/goods";
		    String userName = "root";
		    String password = "kamisama";
		    String driverName = "com.mysql.jdbc.Driver";
		    String strSql = "select * from good";
		    Scanner sc = new Scanner(System.in);
		    	Connection con = null;
		    	Statement pstmt = null;
		    	ResultSet rs = null;
		    	String driverName1 = "com.mysql.jdbc.Driver";
			@Override
			public void mouseClicked(MouseEvent e) {
				try {
					Class.forName(driverName);
			        con = (Connection) DriverManager.getConnection(url, userName, password);
			        pstmt = (Statement) con.createStatement();
			        rs = pstmt.executeQuery(strSql);
			        while (rs.next()) {
			            int id = rs.getInt("id");
			            String name = rs.getString("name");
			            Double price = rs.getDouble("price");
			            int num = rs.getInt("num");
			            String description = rs.getString("description");
			            System.out.print("id=" + id + "  name="+name+ " price="+price+"  num=" + num+" description="+description+"\n");
			            String str = "id=" + id + "  name="+name+ " price="+price+"  num=" + num+" description="+description+"\n";
			            textArea.append(str);
			        }
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}catch (SQLException e1){
					e1.printStackTrace();
				}finally {
		            if (rs != null)
		                try {
		                    rs.close();
		                } catch (Exception e1) {
		                    e1.printStackTrace();
		                }
		            if (pstmt != null){
		                try {
		                    pstmt.close();// 关闭语句
		                } catch (Exception e1) {
		                    e1.printStackTrace();
		                }
		            }
		            if (con != null) {
		                try {
		                    con.close();// 关闭连接
		                } catch (Exception e1) {
		                    e1.printStackTrace();
		                }
		            }
				}
			}
		});
		btnNewButton_1.setBounds(20, 342, 279, 49);
		contentPane.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("返回");
		btnNewButton_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Fframe f = new Fframe();
				f.setVisible(true);
				dispose();
			}
		});
		btnNewButton_2.setBounds(309, 343, 93, 48);
		contentPane.add(btnNewButton_2);
		

	}
}
